<%@taglib prefix="itrust" uri="/WEB-INF/tags.tld"%>
<%@page errorPage="/auth/exceptionHandler.jsp"%>

<%@page import="edu.ncsu.csc.itrust.action.LabProcHCPAction"%>
<%@page import="edu.ncsu.csc.itrust.beans.LabProcedureBean"%>
<%@page import="edu.ncsu.csc.itrust.exception.DBException"%>
<%@page import="edu.ncsu.csc.itrust.beans.PatientBean"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientDAO"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.LabProcedureDAO"%>
<%@page import="edu.ncsu.csc.itrust.beans.LabTechProcedureBean"%>
<%@page import="java.util.List"%>
<%@page import="edu.ncsu.csc.itrust.action.EditOfficeVisitAction"%>

<%@include file="/global.jsp"%>

<%
pageTitle = "iTrust - Update Lab Procedure";
%>

<%@include file="/header.jsp"%>

<%
	String headerMessage = "";

LabProcedureBean lbean = null;
long requestID = 0L;
String lpid = request.getParameter("ID");

if (lpid != null && !lpid.equals("")) {
	try {		
		requestID = Long.parseLong(lpid);
		lbean = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
	} catch (Exception e) {
		e.printStackTrace();
	}
}	
	LabProcedureDAO lpDAO = prodDAO.getLabProcedureDAO();
	boolean formIsFilled = request.getParameter("formIsFilled") != null
	&& request.getParameter("formIsFilled").equals("true");
	
	if (formIsFilled) {
		LabProcedureBean b = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
		try{		
			LabProcedureBean bean = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
			String newltid = request.getParameter("addLtId");
			String commentary = request.getParameter("Commentary");
			if( newltid != null && !"".equals(newltid) ){
				lpDAO.updateAssignedTech(requestID, new Long(newltid));
				loggingAction.logEvent(TransactionType.LAB_REASSIGN, loggedInMID.longValue(), requestID,"Former: "+bean.getLtID()+" New: "+newltid);
			}
			if( commentary != null && !"".equals(commentary) ){
				lpDAO.completeProcedure(requestID, commentary );
				loggingAction.logEvent(TransactionType.LAB_RESULT_ADD, loggedInMID.longValue(), requestID , "LT: "+bean.getLtID()+" PID: "+bean.getPid()+" Commentary: "+commentary);
			}	
			%>
<span>Information Updated Successfully</span>
<% } catch(DBException e){
			e.printStackTrace();
		}
	} else {
		loggingAction.logEvent(TransactionType.LAB_RESULTS_VIEW, loggedInMID.longValue(), requestID, "HCP viewed procedure id: "+lpid);
	}	
%>
<table class="fTable">
	<tr>
		<th colspan="11">Lab Procedures</th>
	</tr>
	<tr>
		<th>Patient</th>
		<th>Lab Code</th>
		<th>Priority</th>
		<th>Rights</th>
		<th>Status</th>
		<th>Commentary</th>
		<th>Results</th>
		<th>OfficeVisitID</th>
		<th>Assigned Lab Tech</th>
		<th>Updated Date</th>

	</tr>
	<%LabProcedureBean bean = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
		PatientBean patient = new PatientDAO(prodDAO).getPatient(bean.getPid());
		%>
	<% String ltname = "Unassigned"; %>
	<% if(bean.getLtID() != 0){ %>
	<% ltname = prodDAO.getPersonnelDAO().getName(bean.getLtID()); %>
	<% } %>
	<tr>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (patient.getFullName())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getLoinc())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getPriority())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getRights())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getStatus())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getCommentary())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getResults())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getOvID())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (ltname)) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getTimestamp())) %></td>
	</tr>
</table>

<form
	action="UpdateLabProc.jsp?ID=<%= StringEscapeUtils.escapeHtml("" + (lpid)) %>"
	&message="Updated Laboratory Procedure" method="post"><input
	type="hidden" name="formIsFilled" value="true"> <br />
<br />
<table align="center">
	<tr>
		<td><%= StringEscapeUtils.escapeHtml("" + (headerMessage )) %></td>
	</tr>
</table>
<% if(bean.getStatus().equals("IN TRANSIT") || bean.getStatus().equals("RECEIVED") ) { %>
<table class="fTable">
	<tr>
		<th colspan="7" style="width: 838px">Lab Tech List</th>
	</tr>
	<tr class="subHeader">
		<td></td>
		<td>Name</td>
		<td>Specialty</td>
		<td>Pending</td>
		<td>Priority 1</td>
		<td>Priority 2</td>
		<td>Priority 3</td>
	</tr>
	<% EditOfficeVisitAction action = new EditOfficeVisitAction(prodDAO, loggedInMID, ""+lbean.getPid(), ""+lbean.getOvID()); %>
	<% List<LabTechProcedureBean> techbeans = action.getAllLabTechs(); %>
	<% if(techbeans.size() == 0 ){ %>
	<tr>
		<td align="center" colspan="7">No Lab Techs on Record</td>
	</tr>
	<% } else { %>
	<% for(LabTechProcedureBean ltbean: techbeans ){ %>
	<tr>
		<td><input type="radio" id="addLtId" name="addLtId"
			value="<%=ltbean.getLtId()%>"></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getName())) %></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getSpecialty())) %></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getTotalProcedures())) %></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getTotalP1())) %></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getTotalP2())) %></td>
		<td align="center"><%= StringEscapeUtils.escapeHtml("" + (ltbean.getTotalP3())) %></td>
	</tr>
	<% } %>
	<% } %>
</table>
<% } else { %> The Reassignment of Lab Technicians is not available past
the TESTING state<br />
<br />
<% } %> <br />
<table>
	<tr>
		<% if( bean.getStatus().equals("PENDING") ) { %>
		<td>Commentary:</td>
		<td><textarea name="Commentary"></textarea> <% }else { %> Adding
		Commentary is only available in the PENDING state<br />
		<% } %>
		
	</tr>
	<tr>
		<td colspan=2 align=center><input type="submit"
			style="font-size: 16pt; font-weight: bold;" value="Update"></td>
	</tr>
</table>
</form>
<br />

<a href="../hcp/LabProcHCP.jsp">Go to View Laboratory Procedures</a>
<br />
<br />
<a
	href="../hcp-uap/editOfficeVisit.jsp?ovID=<%= StringEscapeUtils.escapeHtml("" + (lbean.getOvID())) %>">Go
Back to Office Visit</a>

<%@include file="/footer.jsp"%>
